package com.online.servlet.user;

import com.online.pojo.Bill;
import com.online.pojo.User;
import com.online.pojo.menu;
import com.online.service.user.Impl.UserServiceImpl;
import com.online.service.user.Impl.billServiceImpl;
import com.online.service.user.Impl.merchantServiceImpl;
import com.online.service.user.UserService;
import com.online.service.user.billService;
import com.online.service.user.merchantService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class addBillServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id= Integer.parseInt(req.getParameter("id"));
        String user_name=req.getParameter("user_name");
        String shop_name=req.getParameter("shop_name");
        merchantService merchantService=new merchantServiceImpl();
        menu menu=new menu();
        try {
             merchantService.select(menu,id);
            merchantService.insert(menu.getShop_name(),menu.getId(),user_name,menu.getPrice(),2);

            billService billService=new billServiceImpl();
            UserService userService=new UserServiceImpl();
            User user= userService.login(user_name);



            List<Bill> bill=new  ArrayList<Bill>();
            billService.select(bill,user_name);
            double value=0;
            int i=0;
            while (i<bill.size() ){
                double price= bill.get(i).getPrice();
                value+=price;
                i++;
            }
            req.setAttribute("value",value);
            req.setAttribute("bill",bill);



            List<menu> list=new ArrayList<menu>();
            userService.showAllmenu(list);
            req.setAttribute("m",list);

            List<Bill> billed=new  ArrayList<Bill>();
            billService.selectBilled(billed,user_name);
            double Billedvalue=0;
            int q=0;
            while (q<billed.size() ){
                double prices= billed.get(q).getPrice();
                Billedvalue+=prices;
                q++;
            }
            req.setAttribute("Billedvalue",Billedvalue);
            req.setAttribute("billed",billed);

            req.setAttribute("user_name",user.getUser_name());
            req.setAttribute("account",user.getAccount());
            System.out.println(bill);
            req.getRequestDispatcher("/jsp/orderpage.jsp").forward(req,resp);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
